package com.parkingspace.controller;


import com.parkingspace.entity.ParkingInformation;
import com.parkingspace.entity.Property;
import com.parkingspace.service.impl.ParkingInformationServiceImpl;
import com.parkingspace.service.impl.PropertyServiceImpl;
import com.parkingspace.util.Constants;
import com.parkingspace.util.ResultVO;

import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车位信息表 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/parking-information")
public class ParkingInformationController {

    @Resource
    private ParkingInformationServiceImpl parkingInformationService;

    @Resource
    private PropertyServiceImpl propertyService;

    @PutMapping("/updShare")
    public ResultVO updShare(Property property, ParkingInformation parkingInformation) {
        System.out.println("物业分配比例进来了1"+property.getPropertyId()+":"+property.getPropertyShare());
        System.out.println("车位分配比例进来了2："+parkingInformation.getParkingProportion());
        //ResultVO resultVO;
        boolean flag;

        //根据物业id修改物业分配比例
        boolean f1 = propertyService.updPropertyShareById(property);


        //根据物业id查到该物业的所有车位，并修改这些车位的抽成
        List<ParkingInformation> parkingInformationList = parkingInformationService.getParkingByPid(property.getPropertyId());
         int j=0;
        for (ParkingInformation information : parkingInformationList) {
            information.setParkingProportion(parkingInformation.getParkingProportion());
            boolean f2= parkingInformationService.updateShareById(information);
            System.out.println("f2:"+f2);
            j++;
        }

        if (f1=true&&j==parkingInformationList.size()){
            return new ResultVO(Constants.OPER_SUCCESS,"分配成功",true);

        }
        return new ResultVO(Constants.OPER_FAILURE,"分配失败",false);
//    return new ResultVO(Constants.OPER_FAILURE,"删除失败",false);
    }



}

